package view;

import engine.Simulation;
import model.Model;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.*;
import javafx.stage.*;

/**
 * Window of the actual application; the user interface is divided in tabs.
 *
 * @author Ricardo Cruz {@literal <ricardo.pdm.cruz@gmail.com>}
 * @since  2014-11-13
 */
public class Window extends Application
{
	private TabView view;

	public void start(Stage stage) {
		stage.setOnCloseRequest(ev -> {
			// we create the simulation thread that may still be running
			// -- force exit
			Platform.exit();
			System.exit(0);
		});
		Simulation.create(Model.getCurrentModel());

		TabPane tabs = new TabPane();
		tabs.getTabs().addAll(
			new TabParameters(this),
			new TabLogic(this),
			view = new TabView(this));

		Scene scene = new Scene(tabs);
		scene.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
			if(ev.getCode() == KeyCode.ESCAPE)
				System.exit(0);
		});
		stage.getIcons().add(new Image(getClass().getResourceAsStream("/icon.png")));
		scene.getStylesheets().add(getClass().getResource("/hivsim.css").toExternalForm());
		stage.setTitle("HIVSIM");
		stage.setScene(scene);
		stage.sizeToScene();
		stage.show();
		tabs.requestFocus();
	}

	public void setModel(int model) {
		Simulation.create(model);
		view.reset();
	}
}
